import async from 'async';
type QQ = () => Promise<void>;
const q = async.queue<QQ>(async (task) => {
    await task();
}, 1);

q.error((err, task) => {
    console.log('==========', err, task)
});


const random = (min: number, max: number) => {
    let n = Math.round(Math.random() * (max - min)) + min;
    return n;
}
const parseObjectKeyValue = (keys: string[], obj: { [key: string]: any }) => {
    let parseStr = '';
    for (let i = 0, len = keys.length; i < len; i++) {
        const key = keys[i];
        let value = obj[key];
        if (Array.isArray(value)) {
            value = value.join('');
        } else if (typeof value === 'object') {
            value = parseObjectKeyValue(Object.keys(value).sort(), value);
        }
        parseStr += (key + value);
    }

    return parseStr;
}

const randomStr = (length: number = 10) => {
    let str = '';
    let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for (let i = 0; i < length; i++) {
        str += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return str;
}
const showToast = (msg: string) => {
    ElMessage.info({
        message: msg,
        showClose: true,
    });


}

const showConfirm = (msg: string, showCancelButton: boolean = false) => {
    return ElMessageBox.confirm(msg, {
        showCancelButton,
        showClose: false,
        closeOnClickModal: false
    });
}


const showLoading = () => {
    return ElLoading.service({
        text: "请稍后",
        spinner: "el-icon-loading",
        background: ' rgba(0, 0, 0, 0.2)'
    });
}

const fromDateStr = (val: any) => {
    if (typeof val == 'number') {
        if (val.toString().length == 10) {
            val *= 1000;
        }
    }
    const date = new Date(val);

    const y = date.getFullYear();
    const m = date.getMonth();
    const d = date.getDate();
    const h = date.getHours();
    const i = date.getMinutes();
    const s = date.getSeconds();

    return `${y}-${m < 10 ? '0' + m : m}-${d < 10 ? '0' + d : d} ${h < 10 ? '0' + h : h}:${i < 10 ? '0' + i : i}:${s < 10 ? '0' + s : s}`;
}


const parseNick = (nick: any) => {
    if (!nick) {
        return '';
    }
    nick = unescape(nick).split('');
    const last = nick[nick.length - 1] || '';
    const nick1 = nick[0] || '';
    if (nick.length > 3) {
        nick = [nick1, nick[1], '***', last]
    } else if (nick.length == 3) {
        nick = [nick1, '***', last];
    } else {
        nick = [nick1, '***'];
    }

    return nick.join('');

}



const countdown = (time: number, nowTime: number) => {
    if (time.toString().length == 10) {
        time *= 1000;
    }
    if (time > 0 && time > nowTime) {
        const lefttime = time - nowTime;
        const d = Math.floor(lefttime / (1000 * 60 * 60 * 24));
        const h = Math.floor(lefttime / (1000 * 60 * 60) % 24);
        const i = Math.floor(lefttime / (1000 * 60) % 60);
        const s = Math.floor(lefttime / 1000 % 60);

        if (d == 0 && h == 0 && i == 0 && s == 0) {
            return '';
        } else {
            return `${d}天${h < 10 ? '0' + h : h}:${i < 10 ? '0' + i : i}:${s < 10 ? '0' + s : s}`;
        }
    }
    return '';
}


const cache = {
    get(key: string) {
        const data = window.api.cache.get(key) || '{"value":null}';
        const obj = JSON.parse(data);
        return obj.value;
    },
    set(key: string, val: any, expire: number = 0) {
        const data = JSON.stringify({
            value: val,
        });
        return window.api.cache.set(key, data, expire);
    }
}


export { random, randomStr, parseObjectKeyValue, showToast, showConfirm, showLoading, fromDateStr, parseNick, countdown, cache }